const app = getApp();
// 初始化数据库
const db = wx.cloud.database();
const suggest = db.collection("suggest");
// packageMine/pages/suggest/suggest.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme: 1,
    hide: false,
    o_openid: '',
    textarea: '',

    navHeight: app.globalData.navBarHeight,
    menuHeight: app.globalData.menuHeight,
    menuRight: app.globalData.menuRight,
    menuTop: app.globalData.menuTop,
    screenHeight: app.globalData.screenHeight,
    screenWidth: app.globalData.screenWidth
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  // 获取输入内容
  inputstr(e) {
    var addContent = e.detail.value;
    addContent = addContent.trim();
    this.setData({
      addContent,
    });
  },

  addMsg() {
    const that = this;
    const { addContent } = that.data;
    const avatarUrl = wx.getStorageSync('avatarUrl');
    const nickname = wx.getStorageSync('nickname');
    if (addContent) {
      wx.showLoading({
        title: '提交中...',
      })
      suggest.add({
        data: {
          content: addContent,
          avatarUrl,
          nickname
        },
        success: () => {
          wx.showToast({
            title: '提交成功',
            icon: 'success'
          })

          that.setData({
            addContent: ''
          })
        },
        fail: () => { },
        complete: () => {
          wx.hideLoading();
        }
      })
    } else {
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const o_openid = wx.getStorageSync("o_openid");
    this.setData({
      theme: app.globalData.theme,
      hide: app.globalData.switch,
      o_openid,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})